<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;

/**
 * 文章模型
 */
class CourseCollections extends ModelBase
{
    protected $insert = ['create_time'];

    public function getImageAttr(){
        return get_picture_url($this->cover_id,false);
    }


    public function getVideoUrlAttr(){
        if($this->type!=3){
            return get_file_url($this->file_id,false);
        }else{
            return $this->video_url;
        }

    }



    /**
     * 连课堂表
     */
    public function course(){
        return $this->belongsTo('Course','course_id','id');
    }

    /**
     * 连留言表
     */
    public function reply(){
        return $this->hasMany('CourseCollectionsComment','collect_id','id');
    }

    /**
     * 时间自动完成
     */
    public function setCreateTimeAttr()
    {

        return TIME_NOW;
    }

    /**
     * 获取状态信息
     */
    public function getTypeTextAttr(){

        $arr=[ '','视频','音频','直播'];
        return lang($arr[$this->data['type']]);
    }

    public function getPositionAttr(){
        $count=self::where(['id'=>['lt',$this->id],'course_id'=>$this->course_id])->count();
        $count+=1;
        if($count<9){

            return  '0'.$count;
        }else{
            return $count.'';
        }
    }


    /**
     *是否点赞
     */
    public function zan($uid)
    {

        if(CourseCollectionsFabulous::where(['user_id'=>$uid,'collect_id'=>$this->id])->find()){
            return 1;
        }else{
            return 0;
        }
    }
    public function  getContentAttr($value){
        if(isset($_REQUEST['lang']) && $_REQUEST['lang']!='zh-cn' &&  $_REQUEST['lang'] ){
            return   baidu_translate($value);
        }
        return $value;

    }
}
